package com.example.wangzh.developkit.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

/**
 * Created by wangzh on 2017/12/5.
 */

public abstract class BaseSelectAdapter<VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {
    //内部接口，定义点击方法以及长按方法
    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    protected int selectItemId = 0;
    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(VH holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectItemId(position);
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemLongClick(v, position);
                }
                return false;
            }
        });
        customBindViewHolder(holder, position);
    }

    protected abstract void customBindViewHolder(VH holder, int position);

    public void setSelectItemId(int selectItemId) {
        if (this.selectItemId != selectItemId) {
            this.selectItemId = selectItemId;
            notifyDataSetChanged();
        }
    }

    public int getSelectItemId() {
        return selectItemId;
    }

}
